{{define "tool/gen/list"}}
<html lang="zh">
<head>
    {{template "header" (OssUrl)}}
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="gen-form">
                <div class="select-list">
                    <ul>
                        <li>
                            表名称：<input type="text" name="tableName"/>
                        </li>
                        <li>
                            表描述：<input type="text" name="tableComment"/>
                        </li>
                        <li class="select-time">
                            <label>表时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            {{getPermiButton .uid "tool:gen:code" "batchGenCode()" "生成" "btn btn-success multiple disabled" "fa fa-download"}}
            {{getPermiButton .uid "tool:gen:code" "importTable()" "导入" "btn btn-info" "fa fa-upload"}}
            {{getPermiButton .uid "tool:gen:edit" "$.operate.editTab()" "修改" "btn btn-primary single disabled" "fa fa-edit"}}
            {{getPermiButton .uid "tool:gen:remove" "$.operate.removeAll()" "删除" "btn btn-danger multiple disabled" "fa fa-remove"}}
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
{{template "footer" (OssUrl)}}
<script type="text/javascript">
    var prefix = ctx + "/tool/gen";
    var editFlag = '{{hasPermi .uid "tool:gen:edit"}}';
    var removeFlag = '{{hasPermi .uid "tool:gen:remove"}}';
    var previewFlag = '{{hasPermi .uid "tool:gen:preview"}}';
    var codeFlag = '{{hasPermi .uid "tool:gen:code"}}';

    $(function () {
        var options = {
            url: prefix + "/list",
            updateUrl: prefix + "/edit?id={id}",
            removeUrl: prefix + "/remove",
            sortName: "create_time",
            sortOrder: "desc",
            showExport: true,
            modalName: "生成配置",
            rememberSelected: true,
            uniqueId: "table_id",
            columns: [{
                field: 'state',
                checkbox: true
            },
                {
                    field: 'table_id',
                    title: '编号',
                    visible: false
                },
                {
                    title: "序号",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'table_name',
                    title: '表名称',
                    sortable: true
                },
                {
                    field: 'business_name',
                    title: '业务名称',
                    sortable: true
                },

                {
                    field: 'class_name',
                    title: '实体类名称',
                    sortable: true
                },
                {
                    field: 'table_comment',
                    title: '表描述',
                    sortable: true
                },
                {
                    field: 'update_time',
                    title: '更新时间',
                    sortable: true,
                    formatter:function (value,row,index) {
                        return value;
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs ' + previewFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.table_id + '\')"><i class="fa fa-search"></i>预览</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.table_id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.table_id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="genCode(\'' + row.table_name + '\',\'' + row.table_id + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
                        actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="importMenuSql(\'' + row.table_name + '\',\'' + row.table_id + '\')"><i class="fa fa-bug"></i>创建菜单</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 预览代码
    function preview(tableId) {
        var preViewUrl = prefix + "/preview?tableId=" + tableId;
        $.modal.loading("正在加载数据，请稍后...");
        $.get(preViewUrl, function (result) {
            if (result.code == web_status.SUCCESS) {
                var items = [];
                $.each(result.data, function (index, value) {
                    value = value.replace(/</g, "&lt;");
                    value = value.replace(/>/g, "&gt;");
                    var templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
                    items.push({
                        title: templateName, content: "<pre class=\"layui-code\">" + value + "</pre>"
                    })
                });
                top.layer.tab({
                    area: ['98%', '90%'],
                    shadeClose: true,
                    tab: items
                });
            } else {
                $.modal.alertError(result.msg);
            }
            $.modal.closeLoading();
        });
    }

    // 生成代码
    function importMenuSql(tableName, tableId) {
        $.modal.confirm("确定导入" + tableName + "表的菜单吗？", function () {
            var url = prefix + "/execSqlFile?tableId=" + tableId;
            $.modal.loading("正在执行相关SQL,请稍后...");
            $.get(url, function (result) {
                if (result.code == web_status.SUCCESS) {
                        $.modal.confirm("操作成功，是否刷新界面以查看最新菜单？" + tableName + "表的菜单吗？", function () {
                            parent.location.reload();
                        });
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        })
    }


    // 生成代码
    function genCode(tableName, tableId) {
        $.modal.confirm("确定要生成" + tableName + "表代码吗？", function () {
            var url = prefix + "/genCode?tableId=" + tableId;
            $.modal.loading("正在生成代码,请稍后...");
            $.get(url, function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("生成成功，请在生成目录查看")
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        })
    }
    //批量生成代码
    function batchGenCode() {
        var rows = $.table.selectColumns("tableName");
        if (rows.length == 0) {
            $.modal.alertWarning("请选择要生成的数据");
            return;
        }
        $.modal.confirm("确认要生成选中的" + rows.length + "条数据吗?", function () {
            location.href = prefix + "/batchGenCode?tables=" + rows;
            layer.msg('执行成功,正在生成代码请稍后…', {icon: 1});
        });
    }

    // 导入表结构
    function importTable() {
        var importTableUrl = prefix + "/importTable";
        $.modal.open("导入表结构", importTableUrl);
    }
</script>
</body>
</html>
{{end}}